import { query } from '../services/check';

export default {
  namespace: 'check',
  state: {},
  effects: {
    *getCustomers({ payload }, { call, put }) {
      const { data: { message, default_order, default_customer } } = yield call(query, 'customers');
      message === 'success' ? payload.success() : payload.failed();
      yield put({ type: 'update', default_order, default_customer });
    },
    *getSuppliers({ payload }, { call, put }) {
      const { data: { message, liabilities_GRN, liabilities_suppliers } } = yield call(query);
      message === 'success' ? payload.success() : payload.failed();
      yield put({ type: 'update', liabilities_GRN, liabilities_suppliers });
    }
  },
  reducers: {
    update(state, action) {
      const newState = {
        default_order: action.default_order,
        default_customer: action.default_customer,
        liabilities_GRN: action.liabilities_GRN,
        liabilities_suppliers: action.liabilities_suppliers
      }
      return { ...state, ...newState }
    }
  }
}